﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;




namespace backgammon_jovanovic
{
    public partial class fGameWindow : Form
    {
        PictureBox[,] pieces;
        Button[,] buttons;
        gameBoard board = new gameBoard();
        int movex;


        public fGameWindow()
        {
            InitializeComponent();

            // Test: Remove or comment out when necessary
            int[,] piecePositions = new int[28, 2] { {2,0}, {1,2}, {0,0}, {0,0}, {0,0}, {0,0}, {2,5}, {0,0}, {2,3}, {0,0}, {0,0}, {0,0}, {1,5}, {2,5}, {0,0}, {0,0},
                                                    {0,0}, {1,3}, {0,0}, {1,5}, {0,0}, {0,0}, {0,0}, {0,0}, {2,2}, {1,0}, {1,0}, {2,7} };

            #region Pieces Array
            pieces = new PictureBox[24, 15]{ 
                {pbS01P01, pbS01P02, pbS01P03, pbS01P04, pbS01P05, pbS01P06, pbS01P07, pbS01P08, pbS01P09, pbS01P10, pbS01P11, pbS01P12, pbS01P13, pbS01P14, pbS01P15},
                {pbS02P01, pbS02P02, pbS02P03, pbS02P04, pbS02P05, pbS02P06, pbS02P07, pbS02P08, pbS02P09, pbS02P10, pbS02P11, pbS02P12, pbS02P13, pbS02P14, pbS02P15},
                {pbS03P01, pbS03P02, pbS03P03, pbS03P04, pbS03P05, pbS03P06, pbS03P07, pbS03P08, pbS03P09, pbS03P10, pbS03P11, pbS03P12, pbS03P13, pbS03P14, pbS03P15},
                {pbS04P01, pbS04P02, pbS04P03, pbS04P04, pbS04P05, pbS04P06, pbS04P07, pbS04P08, pbS04P09, pbS04P10, pbS04P11, pbS04P12, pbS04P13, pbS04P14, pbS04P15},
                {pbS05P01, pbS05P02, pbS05P03, pbS05P04, pbS05P05, pbS05P06, pbS05P07, pbS05P08, pbS05P09, pbS05P10, pbS05P11, pbS05P12, pbS05P13, pbS05P14, pbS05P15},
                {pbS06P01, pbS06P02, pbS06P03, pbS06P04, pbS06P05, pbS06P06, pbS06P07, pbS06P08, pbS06P09, pbS06P10, pbS06P11, pbS06P12, pbS06P13, pbS06P14, pbS06P15},
                {pbS07P01, pbS07P02, pbS07P03, pbS07P04, pbS07P05, pbS07P06, pbS07P07, pbS07P08, pbS07P09, pbS07P10, pbS07P11, pbS07P12, pbS07P13, pbS07P14, pbS07P15},
                {pbS08P01, pbS08P02, pbS08P03, pbS08P04, pbS08P05, pbS08P06, pbS08P07, pbS08P08, pbS08P09, pbS08P10, pbS08P11, pbS08P12, pbS08P13, pbS08P14, pbS08P15},
                {pbS09P01, pbS09P02, pbS09P03, pbS09P04, pbS09P05, pbS09P06, pbS09P07, pbS09P08, pbS09P09, pbS09P10, pbS09P11, pbS09P12, pbS09P13, pbS09P14, pbS09P15},
                {pbS10P01, pbS10P02, pbS10P03, pbS10P04, pbS10P05, pbS10P06, pbS10P07, pbS10P08, pbS10P09, pbS10P10, pbS10P11, pbS10P12, pbS10P13, pbS10P14, pbS10P15},
                {pbS11P01, pbS11P02, pbS11P03, pbS11P04, pbS11P05, pbS11P06, pbS11P07, pbS11P08, pbS11P09, pbS11P10, pbS11P11, pbS11P12, pbS11P13, pbS11P14, pbS11P15},
                {pbS12P01, pbS12P02, pbS12P03, pbS12P04, pbS12P05, pbS12P06, pbS12P07, pbS12P08, pbS12P09, pbS12P10, pbS12P11, pbS12P12, pbS12P13, pbS12P14, pbS12P15},
                {pbS13P01, pbS13P02, pbS13P03, pbS13P04, pbS13P05, pbS13P06, pbS13P07, pbS13P08, pbS13P09, pbS13P10, pbS13P11, pbS13P12, pbS13P13, pbS13P14, pbS13P15},
                {pbS14P01, pbS14P02, pbS14P03, pbS14P04, pbS14P05, pbS14P06, pbS14P07, pbS14P08, pbS14P09, pbS14P10, pbS14P11, pbS14P12, pbS14P13, pbS14P14, pbS14P15},
                {pbS15P01, pbS15P02, pbS15P03, pbS15P04, pbS15P05, pbS15P06, pbS15P07, pbS15P08, pbS15P09, pbS15P10, pbS15P11, pbS15P12, pbS15P13, pbS15P14, pbS15P15},
                {pbS16P01, pbS16P02, pbS16P15, pbS16P04, pbS16P05, pbS16P06, pbS16P07, pbS16P08, pbS16P15, pbS16P10, pbS16P11, pbS16P12, pbS16P13, pbS16P14, pbS16P15},
                {pbS17P01, pbS17P02, pbS17P03, pbS17P04, pbS17P05, pbS17P06, pbS17P07, pbS17P08, pbS17P09, pbS17P10, pbS17P11, pbS17P12, pbS17P13, pbS17P14, pbS17P15},
                {pbS18P01, pbS18P02, pbS18P03, pbS18P04, pbS18P05, pbS18P06, pbS18P07, pbS18P08, pbS18P09, pbS18P10, pbS18P11, pbS18P12, pbS18P13, pbS18P14, pbS18P15},
                {pbS19P01, pbS19P02, pbS19P03, pbS19P04, pbS19P05, pbS19P06, pbS19P07, pbS19P08, pbS19P09, pbS19P10, pbS19P11, pbS19P12, pbS19P13, pbS19P14, pbS19P15},
                {pbS20P01, pbS20P02, pbS20P03, pbS20P04, pbS20P05, pbS20P06, pbS20P07, pbS20P08, pbS20P09, pbS20P10, pbS20P11, pbS20P12, pbS20P13, pbS20P14, pbS20P15},
                {pbS21P01, pbS21P02, pbS21P03, pbS21P04, pbS21P05, pbS21P06, pbS21P07, pbS21P08, pbS21P09, pbS21P10, pbS21P11, pbS21P12, pbS21P13, pbS21P14, pbS21P15},
                {pbS22P01, pbS22P02, pbS22P03, pbS22P04, pbS22P05, pbS22P06, pbS22P07, pbS22P08, pbS22P09, pbS22P10, pbS22P11, pbS22P12, pbS22P13, pbS22P14, pbS22P15},
                {pbS23P01, pbS23P02, pbS23P03, pbS23P04, pbS23P05, pbS23P06, pbS23P07, pbS23P08, pbS23P09, pbS23P10, pbS23P11, pbS23P12, pbS23P13, pbS23P14, pbS23P15},
                {pbS24P01, pbS24P02, pbS24P03, pbS24P04, pbS24P05, pbS24P06, pbS24P07, pbS24P08, pbS24P09, pbS24P10, pbS24P11, pbS24P12, pbS24P13, pbS24P14, pbS24P15},
            };
            #endregion

            #region Buttons Array
            buttons = new Button[24, 1]{ 
                {bMoveTo01},
                {bMoveTo02},
                {bMoveTo03},
                {bMoveTo04},
                {bMoveTo05},
                {bMoveTo06},
                {bMoveTo07},
                {bMoveTo08},
                {bMoveTo09},
                {bMoveTo10},
                {bMoveTo11},
                {bMoveTo12},
                {bMoveTo13},
                {bMoveTo14},
                {bMoveTo15},
                {bMoveTo16},
                {bMoveTo17},
                {bMoveTo18},
                {bMoveTo19},
                {bMoveTo20},
                {bMoveTo21},
                {bMoveTo22},
                {bMoveTo23},
                {bMoveTo24}
            };
            #endregion

            // Alter as needed
            setPieces(board.showBoard(4));
            playGame();
        }




        #region Dice Rolling

        private void bRollDice1_Click(object sender, EventArgs e)
        {
            board.rollDice();
            int die1 = board.showDie(1);
            int die2 = board.showDie(2);
            switch (die1)
            {
                case 1:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_1;
                    break;
                case 2:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_2;
                    break;
                case 3:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_3;
                    break;
                case 4:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_4;
                    break;
                case 5:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_5;
                    break;
                case 6:
                    pbDie1.Image = global::backgammon_jovanovic.Properties.Resources.die_6;
                    break;
            }

            switch (die2)
            {
                case 1:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_1;
                    break;
                case 2:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_2;
                    break;
                case 3:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_3;
                    break;
                case 4:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_4;
                    break;
                case 5:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_5;
                    break;
                case 6:
                    pbDie2.Image = global::backgammon_jovanovic.Properties.Resources.die_6;
                    break;
            }
            setLockRollButton(1, false);
            setPieces(board.showBoard(1));
        }

        private void bRollDice2_Click(object sender, EventArgs e)
        {
            board.rollDice();
            int die1 = board.showDie(1);
            int die2 = board.showDie(2);

            switch (die1)
            {
                case 1:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_1;
                    break;
                case 2:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_2;
                    break;
                case 3:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_3;
                    break;
                case 4:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_4;
                    break;
                case 5:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_5;
                    break;
                case 6:
                    pbDie3.Image = global::backgammon_jovanovic.Properties.Resources.die_6;
                    break;
            }

            switch (die2)
            {
                case 1:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_1;
                    break;
                case 2:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_2;
                    break;
                case 3:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_3;
                    break;
                case 4:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_4;
                    break;
                case 5:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_5;
                    break;
                case 6:
                    pbDie4.Image = global::backgammon_jovanovic.Properties.Resources.die_6;
                    break;
            }
            setLockRollButton(2, false);
            setPieces(board.showBoard(1));
        }

        #endregion

        #region GUI Operations

        public void toggleVisibility(PictureBox piece, bool vis)
        {
            piece.Visible = vis;
        }

        public void setLockRollButton(int player, bool change)
        {
            switch (player)
            {
                case 1:
                    bRollDice1.Enabled = change;
                    break;
                case 2:
                    bRollDice2.Enabled = change;
                    break;
                default:
                    throw new Exception("Invalid player number passed to lockRollButton() method.");
            }
        }

        public void setBarCounter(int player, int amt)
        {
            switch (player)
            {
                case 1:
                    lP1BarCount.Text = amt.ToString();
                    break;
                case 2:
                    lP2BarCount.Text = amt.ToString();
                    break;
                default:
                    throw new Exception("Invalid player number passed to method setBarCounter()");
            }
        }

        public void setBearOffCounter(int player, int amt)
        {
            switch (player)
            {
                case 1:
                    lP1BearOffCounter.Text = amt.ToString();
                    break;
                case 2:
                    lP2BearOffCounter.Text = amt.ToString();
                    break;
                default:
                    throw new Exception("Invalid player integer passed in method setBearOffCounter()");
            }
        }

        /// <summary>
        /// Changes the color of the pieces in a given space
        /// </summary>
        /// <param name="player">The number of the player: 1 for Player 1, and anything else for Player 2</param>
        /// <param name="space">The number of the space between 1 and 24</param>
        public void setSpaceColor(int player, int space)
        {
            Image checker;

            if (player == 1) checker = global::backgammon_jovanovic.Properties.Resources.whitechecker;
            else checker = global::backgammon_jovanovic.Properties.Resources.blackchecker;

            for (int i = 0; i < pieces.GetLength(1); i++)
            {
                pieces[space - 1, i].Image = checker;
            }
        }

        public void resetPieces()
        {
            foreach (PictureBox i in pieces) toggleVisibility(i, false);
        }

        public void setPieces(int[,] arr)
        {
            /**
             * Set 0 is P2 bear off
             * Sets 1-24 are the spaces
             * Set 25 if P1 bear off
             * Sets 26 and 27 are P1 bar and P2 bar, respectively
             */


            // Check to make sure array is of proper length
            if (arr.GetLength(0) != 28 && arr.GetLength(1) != 2) throw new Exception("Invalid array passed to setPieces() method.");

            resetPieces();

            // Set P2 Bear Off Counter
            setBearOffCounter(2, arr[0, 1]);

            // Set Piece visibility and color
            for (int i = 1; i <= 24; i++)
            {
                // Set Color of pieces in space
                setSpaceColor(arr[i, 0], i);
                if ((arr[i, 0] == board.playeris()) && (arr[i, 0] != 0))
                    buttons[i - 1, 0].Visible = true;
                else
                    buttons[i - 1, 0].Visible = false;
                // Set Piece visibility for that space
                for (int j = 0; j < arr[i, 1]; j++)
                {
                    toggleVisibility(pieces[i - 1, j], true);
                }
            }

            // Set P1 Bear Off Counter
            setBearOffCounter(1, arr[25, 1]);

            // Set P1 and P2 Bar Counts
            setBarCounter(1, arr[26, 1]);
            setBarCounter(2, arr[27, 1]);
        }

        #endregion


        #region game play
        public void playGame()
        {
            //int play = 1, p2 = 0;
            //string pz = "x";
            setLockRollButton(1, true);
            setLockRollButton(2, false);
            //board.movePeice(1, 1+ board.showDie(1), 1);
            setPieces(board.showBoard(1));
        }

        private void bClick(int btn)
        {

            int[,] arr = new int[1, 2];
            arr = board.moveLegal(btn, board.playeris());
            //set start


            if (movex == 0)
            {
                for (int i = 1; i <= 24; i++)
                {

                    if ((arr[0, 0] == i) || (arr[0, 1] == i) || (btn == i))
                        buttons[i - 1, 0].Visible = true;
                    else
                        buttons[i - 1, 0].Visible = false;

                }
                movex = btn;
            }
            else
            {
                if (btn == movex)//cancle
                {
                    setPieces(board.showBoard(1));
                }
                else
                {

                    board.movePeice(movex, btn, board.playeris());
                    setPieces(board.showBoard(1));
                }

                movex = 0;
            }
            //set end
            if (board.IsALegalMove(board.playeris()) == false)
            {
                setLockRollButton(2 - Math.Abs(board.playeris() - 1), true);
                setLockRollButton(board.playeris(), false);
                setPieces(board.showBoard(1));
            }
        }

        private void startNewGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPieces(board.showBoard(4)); // start new game
            //playGame();
        }

        private void undoAstMoveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            board.undoMove(0);
        }

        private void lToolStripMenuItem_Click(object sender, EventArgs e)
        {
            board.undoMove(1);
        }

        private void loadGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPieces(board.showBoard(2)); // load game
        }

        private void saveGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            setPieces(board.showBoard(3)); // save game
        }


        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region space buttons
        private void bMoveTo01_Click(object sender, EventArgs e)
        {
            bClick(1);
        }

        private void bMoveTo02_Click(object sender, EventArgs e)
        {
            bClick(2);
        }

        private void bMoveTo03_Click(object sender, EventArgs e)
        {
            bClick(3);
        }

        private void bMoveTo04_Click(object sender, EventArgs e)
        {
            bClick(4);
        }

        private void bMoveTo05_Click(object sender, EventArgs e)
        {
            bClick(5);
        }

        private void bMoveTo06_Click(object sender, EventArgs e)
        {
            bClick(6);
        }

        private void bMoveTo07_Click(object sender, EventArgs e)
        {
            bClick(7);
        }

        private void bMoveTo08_Click(object sender, EventArgs e)
        {
            bClick(8);
        }

        private void bMoveTo09_Click(object sender, EventArgs e)
        {
            bClick(9);
        }

        private void bMoveTo10_Click(object sender, EventArgs e)
        {
            bClick(10);
        }

        private void bMoveTo11_Click(object sender, EventArgs e)
        {
            bClick(11);
        }

        private void bMoveTo12_Click(object sender, EventArgs e)
        {
            bClick(12);
        }

        private void bMoveTo13_Click(object sender, EventArgs e)
        {
            bClick(13);
        }

        private void bMoveTo14_Click(object sender, EventArgs e)
        {
            bClick(14);
        }

        private void bMoveTo15_Click(object sender, EventArgs e)
        {
            bClick(15);
        }

        private void bMoveTo16_Click(object sender, EventArgs e)
        {
            bClick(16);
        }

        private void bMoveTo17_Click(object sender, EventArgs e)
        {
            bClick(17);
        }

        private void bMoveTo18_Click(object sender, EventArgs e)
        {
            bClick(18);
        }

        private void bMoveTo19_Click(object sender, EventArgs e)
        {
            bClick(19);
        }

        private void bMoveTo20_Click(object sender, EventArgs e)
        {
            bClick(20);
        }

        private void bMoveTo21_Click(object sender, EventArgs e)
        {
            bClick(21);
        }

        private void bMoveTo22_Click(object sender, EventArgs e)
        {
            bClick(22);
        }

        private void bMoveTo23_Click(object sender, EventArgs e)
        {
            bClick(23);
        }

        private void bMoveTo24_Click(object sender, EventArgs e)
        {
            bClick(24);
        }
        #endregion
    }

}
